package cn.tedu.serializable;

import java.io.*;

/**
 * @author :Colin
 * @version 创建时间: 2021/6/18 10:42
 */
/*本类用于测试序列化和反序列化*/
public class TestSerializable {
    public static void main(String[] args) {
        method();
        method2();
    }

    private static void method2() {
        ObjectInputStream in = null;

        
        try{
            in =  new ObjectInputStream(new FileInputStream("D:\\punk\\1.txt"));
            //创建一个需要被序列化输入的对象

            Object o = in.readObject();
            System.out.println(o);
            System.out.println("恭喜您，反序列化成功");

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("反序列失败");
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method() {
        ObjectOutputStream out = null;
        try{
            out =  new ObjectOutputStream(new FileOutputStream("D:\\punk\\1.txt"));
            //创建一个需要被序列化输出的对象
            Student obj =new Student("lele",14,"快乐星球",'男');
            out.writeObject(obj);
            System.out.println("恭喜您，序列化成功");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("序列失败");
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
